package com.example.moblie.poll;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import com.example.moblie.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SearchListAdapter extends ArrayAdapter<Poll> {
	private final Context context;
	private final ArrayList<Poll> polls;

    private LayoutInflater inflater;

	
	public SearchListAdapter(Context context, int resource,	ArrayList<Poll> polls) {
		super(context, resource, polls);
		
        //this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //this.inflater = LayoutInflater.from(context);
        //Log.v("","");
			System.out.println(polls.size());	
		this.context = context;
		this.polls = polls;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		//LayoutInflater inflater =
		
			System.out.println("wtf");
	
		    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	        //	
	
			if (convertView ==null ) {
				System.out.println("wtf getContext");
			}
			
	
			//RelativeLayout rl = findViewById(R.id.svRL);
			
			View rowView = inflater.inflate(R.layout.search_listview, parent, false);
	
			ImageView imgPoll = (ImageView) rowView.findViewById(R.id.imgPoll);
			TextView tvTitle = (TextView) rowView.findViewById(R.id.tvTitle);
			TextView tvCreator = (TextView) rowView.findViewById(R.id.tvCreator);
			TextView tvDateCreated = (TextView) rowView.findViewById(R.id.tvDateCreated);
			
	
			tvTitle.setText(polls.get(position).getTitle());
			
			System.out.println("wtf aaa");
	
			
			tvCreator.setText(String.valueOf(polls.get(position).getCreatorName()));
			tvDateCreated.setText(String.valueOf(polls.get(position).getDateCreated()));
			loadImage(position, imgPoll);
			
	
			return rowView;

	}

	@Override
	public int getCount(){

	    return polls.size();
	}
	
	private void loadImage(int position, ImageView img) {
		LoadImageTask lit = new LoadImageTask(img);
		lit.execute(position);
	}

	private class LoadImageTask extends AsyncTask<Object, Void, Bitmap> {
		ImageView img;

		public LoadImageTask(ImageView img) {
			this.img = img;
		}

		@Override
		protected Bitmap doInBackground(Object... params) {
			// TODO Auto-generated method stub
			int position = Integer.parseInt(params[0].toString());
			URL url;
			try {
				Log.d("ABC", String.valueOf(position));
				if (!polls.get(position).getImgPath().contains("no_image")) {
					url = new URL(polls.get(position).getImgPath());
					Log.d("URL", url.getPath());
					Bitmap bmp = BitmapFactory.decodeStream(url
							.openConnection().getInputStream());
					return bmp;
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Bitmap result) {
			if(result != null){
				img.setVisibility(1);
				img.setImageBitmap(result);
			}
		}
	}
}
